package com.example.demo.main;

public class StatusFlags {
   // 定义状态位
   private static final int STATUS_BIT_1 = 1 << 4; // 001
   private static final int STATUS_BIT_2 = 1 << 1; // 010
   private static final int STATUS_BIT_3 = 1 << 2; // 100

   private static int flags; // 用于存储状态位的字段

   public StatusFlags() {
      this.flags = 0; // 初始化时没有设置任何位
   }

   // 设置状态位
   public void setBit(int bit) {
      flags |= bit; // 使用按位或运算符设置位
   }

   // 清除状态位
   public void clearBit(int bit) {
      flags &= ~bit; // 使用按位取反再或运算符清除位
   }

   // 检查状态位是否设置
   public boolean isBitSet(int bit) {
      return (flags & bit) == bit; // 使用按位与运算符检查位是否设置
   }

   // 示例使用
   public static void main(String[] args) {
      StatusFlags statusFlags = new StatusFlags();

      statusFlags.setBit(STATUS_BIT_1);
      statusFlags.setBit(STATUS_BIT_2);
      statusFlags.setBit(STATUS_BIT_3);

      System.out.println("Bit 1 is set: " + flags+":" +Integer.toBinaryString(flags) +":"+ statusFlags.isBitSet(STATUS_BIT_1)); // true
      System.out.println("Bit 2 is set: " + flags+":"+Integer.toBinaryString(flags) +":"+ statusFlags.isBitSet(STATUS_BIT_2)); // true
      System.out.println("Bit 3 is set: " + flags+":"+Integer.toBinaryString(flags) +":"+ statusFlags.isBitSet(STATUS_BIT_3)); // false

      statusFlags.clearBit(STATUS_BIT_1);

      System.out.println("Bit 1 is set: " + flags+":"+Integer.toBinaryString(flags) +":"+ statusFlags.isBitSet(STATUS_BIT_1)); // false
      System.out.println("Bit 2 is set: " + flags+":"+Integer.toBinaryString(flags) +":"+ statusFlags.isBitSet(STATUS_BIT_2)); // true
   }
}
